package com.sptpc.drink.controller;

import com.sptpc.drink.pojo.Category;
import com.sptpc.drink.service.CategoryService;
import com.sptpc.drink.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam("name") String name){
        return "hello" + name;
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse queryById(@PathVariable("id")long id){
        return ServerResponse.createBySuccess(categoryService.queryById(id));
    }

    @GetMapping(value = "/",params = "name")
    @ResponseBody
    public ServerResponse queryByName(@RequestParam String name){
        return ServerResponse.createBySuccess("成功",categoryService.queryByName(name));
    }

    @GetMapping(value = "/")
    @ResponseBody
    public ServerResponse queryAll(){
        return ServerResponse.createBySuccess("成功",categoryService.queryAll());
    }

    @PostMapping(value = "/save",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ServerResponse insert(@RequestBody Category category){
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        categoryService.insert(category);
        return ServerResponse.createBySuccessMessage("插入成功");
    }

    @PostMapping(value = "/update",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ServerResponse update(@RequestBody Category category){
        category.setUpdateTime(new Date());
        categoryService.update(category);
        return ServerResponse.createBySuccessMessage("更新成功");
    }

    @GetMapping(value = "/delete",consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ServerResponse delete(@RequestParam("id") long id){
       categoryService.delete(id);
        return ServerResponse.createBySuccessMessage("删除成功");
    }

}
